using System;
using System.Collections.Generic;
using System.Text;
using DotNetMatrix;

namespace ScannerLib.Vector
{
    public class BasicVector
    {

        private double m_x, m_y, m_z;

        public BasicVector() : this(0, 0, 0) { }
        public BasicVector(double x, double y, double z)
        {
            m_x = x;
            m_y = y;
            m_z = z;
        }

        public double X
        {
            get
            {
                return m_x;
            }

            set
            {
                m_x = value;
            }
        }

        public double Y
        {
            get
            {
                return m_y;
            }

            set
            {
                m_y = value;
            }
        }

        public double Z
        {
            get
            {
                return m_z;
            }

            set
            {
                m_z = value;
            }
        }

        public double Length
        {
            get
            {
                return Math.Sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
            }
        }
        public GeneralMatrix ToGeneralMatrix()
        {
            GeneralMatrix result = new GeneralMatrix(3, 1);

            result.SetElement(0, 0, X);
            result.SetElement(1, 0, Y);
            result.SetElement(2, 0, Z);

            return result;
        }

        public static BasicVector FromGeneralMatrix(GeneralMatrix gm)
        {
            return new BasicVector(gm.GetElement(0, 0), gm.GetElement(1, 0), gm.GetElement(2, 0));
        }

        public override string ToString()
        {
            return "[" + m_x + "," + m_y + "," + m_z + "]";
        }

        public static BasicVector CrossProduct(BasicVector v1, BasicVector v2)
        {
            return new BasicVector(
                v1.Y * v2.Z - v1.Z * v2.Y,
                v1.Z * v2.X - v1.X * v2.Z,
                v1.X * v2.Y - v1.Y * v2.X
            );
        }

        public static BasicVector Scale(BasicVector v1, double k)
        {
            return new BasicVector(v1.X * k, v1.Y * k, v1.Z * k);
        }

        /// <summary>
        /// multiply vector by a matrix whose diagonal is given by v1 :)
        /// </summary>
        /// <param name="v1">Vector</param>
        /// <param name="v2">Diagonal</param>
        /// <returns></returns>
        public static BasicVector MultDiag(BasicVector v1,BasicVector v2) 
        {
            return new BasicVector(v1.X * v2.X, v1.Y * v2.Y, v1.Z * v2.Z); 
        }

        public static double DotProduct(BasicVector v1, BasicVector v2)
        {
            return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
        }

        public static BasicVector Sum(BasicVector v1, BasicVector v2)
        {
            return new BasicVector(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);
        }
    }
}
